package cn.dgut.training.service.impl;

import cn.dgut.training.e.Status;
import cn.dgut.training.entity.goods.Category;
import cn.dgut.training.entity.goods.Goods;
import cn.dgut.training.mapper.CategoryMapper;
import cn.dgut.training.mapper.GoodsMapper;
import cn.dgut.training.service.ICategoryService;
import cn.dgut.training.vo.CommonResponse;
import cn.dgut.training.vo.BaseVO;
import cn.dgut.training.vo.CommonResponse;
import cn.dgut.training.vo.goods.CategoryVO;
import cn.dgut.training.vo.goods.GoodsAddCategoryVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public List<Map> getCategoryList(){

        List<Map> list=new ArrayList<>();
        //1.获取1级分类

        List<Category> categories1=categoryMapper.selectList(new LambdaQueryWrapper<Category>()
            .eq(Category::getPid,0));


        for (Category c:categories1) {

            //2.根据1级查询2级分类
            List<Category> categories2=categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                    .eq(Category::getPid,c.getId()));

            //3.根据2级分类查询相应的商品
            List<Goods> goods=new ArrayList<>();
            if(categories2.size()!=0){

                List<Long> lever2=new ArrayList<>();
                for (Category cu:categories2) {
                    lever2.add(cu.getId());
                }
                long[] arr=lever2.stream().mapToLong(t->t.longValue()).toArray();
                if(arr!=null){

                    goods=goodsMapper.getCategoryList(arr);

                }
            }

            Map<String,Object> indexGoods=new HashMap<>();
            indexGoods.put("id",c.getId() );
            indexGoods.put("name",c.getName());
            indexGoods.put("goodsList",goods);
            list.add(indexGoods);
        }

        return list;
    }

    @Override
    public CommonResponse wxIndex(Long id) {
        // 所有一级分类目录
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getPid, 0));

        // 当前一级分类目录
        Category currentCategory = null;
        if (id == null||id.equals(0L)) {
            currentCategory = categoryList.get(0);
        } else {
            currentCategory = categoryMapper.selectById(id);
            if(currentCategory==null){
                currentCategory = categoryList.get(0);
            }
        }

        //查出该一级分类下的2级分类
        List<Category> currentSubCategory = null;
        if (currentCategory != null) {
            currentSubCategory = categoryMapper.selectList(new QueryWrapper<Category>()
                    .lambda()
                    .eq(Category::getPid, currentCategory.getId()));
        }

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("categoryList", categoryList);
        data.put("currentCategory", currentCategory);
        data.put("currentSubCategory", currentSubCategory);
        return CommonResponse.success(data);
    }

    @Override
    public CommonResponse wxCurrent(Long id) {
        // 当前一级分类目录
        Category currentCategory = categoryMapper.selectById(id);
        if (currentCategory == null) {
            return CommonResponse.fail(Status.CATEGORY_ERROR.getCode(), Status.CATEGORY_ERROR.getMessage());
        }
        //查出该一级分类下的2级分类
        List<Category> currentSubCategory = categoryMapper.selectList(new QueryWrapper<Category>()
                .lambda()
                .eq(Category::getPid, currentCategory.getId()));

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("currentCategory", currentCategory);
        data.put("currentSubCategory", currentSubCategory);
        return CommonResponse.success(data);
    }

    public List<GoodsAddCategoryVO> getCa(){

        //Controller获取代码
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid",0);
        List<Category> categoryList=categoryMapper.selectList(queryWrapper);
        List<CategoryVO> categoryVOList = categoryList.stream().map(category -> {
            List<Category> categoryChildrenList = categoryMapper.selectList(new QueryWrapper<Category>().eq("pid",category.getId()));
            List<CategoryVO> categoryVOChildrenList = categoryChildrenList.stream().map(category1 -> {
                CategoryVO categoryVO1=new CategoryVO();
                BeanUtils.copyProperties(category1,categoryVO1);
                return categoryVO1;
            }).collect(Collectors.toList());
            CategoryVO categoryVO=new CategoryVO();
            BeanUtils.copyProperties(category,categoryVO);
            categoryVO.setChildren(categoryVOChildrenList);
            return categoryVO;
        }).collect(Collectors.toList());
        BaseVO<CategoryVO> categoryBaseVO = new BaseVO<>();
        categoryBaseVO.setList(categoryVOList);

        //转换
        List<GoodsAddCategoryVO> cat=new ArrayList<>();

        for (CategoryVO cv:categoryBaseVO.getList()) {
            GoodsAddCategoryVO temp1=new GoodsAddCategoryVO();
            temp1.setValue(cv.getId());
            temp1.setLabel(cv.getName());
            List<GoodsAddCategoryVO> list=new ArrayList<>();
            if(cv.getChildren()!=null) {

                for (CategoryVO c : cv.getChildren()) {
                    Gson gson = new Gson();
                    String s = gson.toJson(c);
                    CategoryVO l=gson.fromJson(s,CategoryVO.class);

                    GoodsAddCategoryVO temp2 = new GoodsAddCategoryVO();
                    temp2.setValue(l.getId());
                    temp2.setLabel(l.getName());
                    list.add(temp2);
                }
                temp1.setChildren(list);
            }
            cat.add(temp1);
        }

        return cat;
    }
}
